import 'package:get/get.dart';

import '../../../../common/api/x_message_assemble_communicate.dart';
import '../../../../common/models/im/im_conversation.dart';
import 'index.dart';

class ConversationPickerController extends GetxController {
  ConversationPickerController();

  final state = ConversationPickerState();

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    fetchConversations();
    super.onReady();
  }

  Future<void> fetchConversations() async {
    var list = await MessageCommunicationService.to.myConversationList();
    if (list != null && list.isNotEmpty) {
      state.conversationList.clear();
      state.conversationList.addAll(list);
    }
  }

  void chooseConversation(IMConversationInfo info) {
    Get.back(result: info);
  }
}
